package it.eng.area118.mdocommon.ebus;

import it.eng.area118.mdocommon.android.util.JsonConverter;

import java.io.Serializable;

import org.jivesoftware.smack.packet.Message;

/**
 * 
 * @author Luigi
 *
 */
public class MessageConverter {

	private static JsonConverter jsonConverter = new JsonConverter();

	public static EBusMessage<Serializable> convert(
			Class<? extends Serializable> clazz, Message msg) {
		EBusMessage<Serializable> ebusMsg = new EBusMessage<Serializable>();
		ebusMsg.setId(msg.getPacketID());
		ebusMsg.setFrom(msg.getFrom());
		ebusMsg.setTo(msg.getTo());
		String json = msg.getBody();
		ebusMsg.setPayload(jsonConverter.convert(clazz, json));
		ebusMsg.setType(msg.getType().name());
		return ebusMsg;
	}

	public static Message convert(EBusMessage<? extends Serializable> eBusMsg,
			Message xmppMsg) {
		xmppMsg.setFrom(eBusMsg.getFrom());
		xmppMsg.setTo(eBusMsg.getTo());
		xmppMsg.setBody(jsonConverter.convert(eBusMsg.getPayload()));
		return xmppMsg;
	}

}
